package cn.Y_Qiu.org.controller;

import cn.Y_Qiu.basic.util.Ajaxresult;
import cn.Y_Qiu.basic.util.PageList;
import cn.Y_Qiu.org.domain.Employee;
import cn.Y_Qiu.org.query.EmployeeQuery;
import cn.Y_Qiu.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    // 添加
    @PutMapping
    public  Ajaxresult addOrUpdate(@RequestBody Employee employee){
        try {
            if (employee.getId() == null) {
                employeeService.add(employee);
            } else {
                employeeService.update(employee);
            }
            return Ajaxresult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return Ajaxresult.me().setMessage("炸了");
        }
    }

    // 删除
    @DeleteMapping("/{id}")
    public Ajaxresult delById(@PathVariable("id") Long id){
        try {
            employeeService.remove(id);
            return Ajaxresult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return Ajaxresult.me()
                    .setMessage("失败");
        }
    }
    // 查单
    @GetMapping("/{id}")
    public Employee queryById(@PathVariable("id") Long id){
        return employeeService.queryById(id);
    }
    // 查全
    @GetMapping
    public List<Employee> queryAll(){
        return employeeService.listAll();
    }
    @PostMapping
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery){
        return employeeService.queryPage(employeeQuery);
    }


    @PatchMapping
    public Ajaxresult patchremove(@RequestBody List<Long> ids){
        System.out.println("到达controller");
        try {
            employeeService.patchremove(ids);
            return Ajaxresult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return Ajaxresult.me().setMessage("失败");
        }

    }
}
